package xyz.eden.cloud.common.web.support.infrastructure.resource;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections4.CollectionUtils;
import xyz.eden.cloud.common.web.support.infrastructure.db.po.BasePO;
import xyz.eden.cloud.common.web.support.infrastructure.resource.param.BaseQryParam;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * BaseQueryResource
 */
public interface BaseQueryResource<PO extends BasePO, QryParam extends BaseQryParam> extends IService<PO> {

    default PO getInfoById(Long id) {
        if (Objects.isNull(id)) {
            return null;
        }

        BaseMapper<PO> baseMapper = this.getBaseMapper();
        return baseMapper.selectById(id);
    }

    /**
     * Get page list by param
     *
     * @param offset
     * @param limit
     * @param qryParam
     * @return
     */
    default Page<PO> getListByPage(int offset, int limit, QryParam qryParam) {
        // 1. 构建查询条件
        LambdaQueryWrapper<PO> queryWrapper = this.builderPageQueryWrapper(qryParam);
        if (Objects.isNull(queryWrapper)) {
            return new Page<>(offset, limit);
        }

        // 2. 查询数据
        Page<PO> page = PageHelper.offsetPage(offset, limit, true);
        BaseMapper<PO> baseMapper = this.getBaseMapper();
        baseMapper.selectList(queryWrapper);
        return page;
    }

    /**
     * Get list by param
     *
     * @param qryParam
     * @return
     */
    default List<PO> getListByParam(QryParam qryParam) {
        // 1. 构建查询条件
        LambdaQueryWrapper<PO> queryWrapper = this.builderPageQueryWrapper(qryParam);
        if (Objects.isNull(queryWrapper)) {
            return Collections.emptyList();
        }

        // 2. 查询数据
        BaseMapper<PO> baseMapper = this.getBaseMapper();
        return baseMapper.selectList(queryWrapper);
    }

    LambdaQueryWrapper<PO> builderPageQueryWrapper(QryParam qryParam);

    default LambdaQueryWrapper<PO> builderBaseQuery(QryParam qryParam) {

        LambdaQueryWrapper<PO> queryWrapper = Wrappers.lambdaQuery();
        if (Objects.isNull(qryParam)) {
            return queryWrapper;
        }

        // LIMIT
        if (Objects.nonNull(qryParam.getLimit())) {
            queryWrapper.last("limit " + qryParam.getLimit());
        }

        // ASC 排序
        if (CollectionUtils.isNotEmpty(qryParam.getAscSortList())) {
            List<String> ascSortList = qryParam.getAscSortList();
            if (ascSortList.contains("id")) {
                queryWrapper.orderByAsc(PO::getId);
            }
        }

        // DESC
        if (CollectionUtils.isNotEmpty(qryParam.getDescSortList())) {
            List<String> ascSortList = qryParam.getAscSortList();
            if (ascSortList.contains("createTime")) {
                queryWrapper.orderByAsc(PO::getCreateTime);
            }
            if (ascSortList.contains("updateTime")) {
                queryWrapper.orderByAsc(PO::getUpdateTime);
            }
        }

        return queryWrapper;
    }
}
